package com.muhuahealth.service.impl;

import com.muhuahealth.dao.BridgeDao;
import com.muhuahealth.entity.BridgeEntity;
import com.muhuahealth.service.BridgeService;
import com.muhuahealth.utils.PageQueryUtil;
import com.muhuahealth.utils.PageResult;
import com.muhuahealth.utils.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author GSK
 * @version 1.0
 * @date 2021/1/24 17:24
 */
@Service
public class BridgeServiceImpl implements BridgeService {

    @Autowired
    BridgeDao bridgeDao;

    @Override
    public PageResult getBridgeListPage(PageQueryUtil queryUtil) {

        //展示分页内容
        List<BridgeEntity> bridgeEntityList = bridgeDao.findBridgeListPage(queryUtil);
        //计算总页数
        int totalPage = bridgeDao.getTotalPage(queryUtil);
        //封装分页
        PageResult result = new PageResult(bridgeEntityList, totalPage, queryUtil.getLimit(), queryUtil.getPage());
        return result;
    }

    @Override
    public ServerResponse saveBridgeInfo(BridgeEntity bridgeEntity) {
        int count = bridgeDao.saveBridgeInfo(bridgeEntity);
        if(count != 1){
            return ServerResponse.createError("插入失败！");
        }
        return ServerResponse.createSuccess("插入成功！");
    }

    @Override
    public ServerResponse updateBridgeInfo(BridgeEntity bridgeEntity) {

        BridgeEntity bridge = bridgeDao.selectByPrimaryKey(bridgeEntity.getId());
        if(bridge == null){
            return ServerResponse.createError("data not exist!");
        }

        int updateInfo = bridgeDao.updateBridgeInfoById(bridgeEntity);
        if(updateInfo > 0){
            return ServerResponse.createSuccess("更新成功！");
        }
        return ServerResponse.createError("更新失败！");
    }

    @Override
    public boolean deleteById(Integer id) {

        if(bridgeDao.deleteById(id) > 0){
            return true;
        }
        return false;
    }

    @Override
    public ServerResponse researchBridgeInfo(BridgeEntity bridgeEntity) {

        return bridgeDao.researchBridgeInfo(bridgeEntity);
    }
}
